#include "DownloadCustomSceneCmd.h"

DownloadCustomSceneCmd::DownloadCustomSceneCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    m_sceneIndex = 0;
}

Package DownloadCustomSceneCmd::toPackage()
{
    Package package;
    return package;
}

void DownloadCustomSceneCmd::unPackage(Package package)
{
    m_cmdId = package.id();
    QByteArray body;

    //body = decrypt(package.body(),KeyType::AES);

    quint16 index = 0;
    quint16 strLength;
    m_sceneIndex = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    quint16 sceneSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;


    quint64 sceneId = body.mid(index,8).toHex().toInt(0,16);
    index += 8;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    QString sceneName = QString::fromUtf8(body.mid(index,strLength));
    index += strLength;

    quint16 sceneIcoId = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    quint16 actionSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    QList<ActionStruct> actionList;

    for(int j=0; j<actionSize; j++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString zoneName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString deviceName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString functionName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString valueName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        quint16 zoneCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceTypeCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionValue = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 address = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 delayTime = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        ActionStruct action;
        action.setZoneName(zoneName);
        action.setDeviceName(deviceName);
        action.setFunctionName(functionName);
        action.setValueName(valueName);
        action.setZoneCode(zoneCode);
        action.setDeviceTypeCode(deviceTypeCode);
        action.setDeviceCode(deviceCode);
        action.setFunctionCode(functionCode);
        action.setFunctionValue(functionValue);
        action.setAddress(address);
        action.setDelayTime(delayTime);
        actionList.append(action);
    }

    Q_UNUSED(sceneSize);
    m_customScene.setId(sceneId);
    m_customScene.setName(sceneName);
    m_customScene.setIcoId(sceneIcoId);
    m_customScene.setActionList(actionList);
}




quint16 DownloadCustomSceneCmd::sceneIndex() const
{
    return m_sceneIndex;
}

void DownloadCustomSceneCmd::setSceneIndex(const quint16 &sceneIndex)
{
    m_sceneIndex = sceneIndex;
}
CustomSceneStruct DownloadCustomSceneCmd::customScene() const
{
    return m_customScene;
}

void DownloadCustomSceneCmd::setCustomScene(const CustomSceneStruct &customScene)
{
    m_customScene = customScene;
}






